<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" @Launchform="Launchform5" @findClicked="findData" :formList="formList" />
    <Find :OriginalFilter="{ static: true }" :searchImg="false" @findClicked="findData1" @Launchform="Launchform" :formList="formList1" />
    <el-card shadow="never" :body-style="{ padding: '11px 20px' }" class="mt10">
      <div style="display: flex">
        <div style="width: 30%" class="f16">
          <span style="font-weight: 600">选择指令</span>
          <Table :isLoading="isLoading1" @currentRow="clicktogetdata1" :tableHeader="tableHeader" :tableData="fecthLotNoTableDate" />
          <PageModule
            :PageData="{ pageSizeShow: true, buttonS: true, total: totalL }"
            :layout="'prev, pager'"
            @pageChange="pageChangeL"
            @pageSizeChange="pageSizeChangeL"
          />
        </div>

        <div style="margin-left: 20px" class="f16">
          <span style="font-weight: 600">指令查询</span>
          <Table :isLoading="isLoading2" @currentRow="clicktogetdata" :tableHeader="tableHeader2" :tableData="lotNoQueryParmsTableDate" />
          <PageModule
            :PageData="{ pageSizeShow: true, buttonS: true, total }"
            :layout="'prev, pager'"
            @pageChange="pageChange"
            @pageSizeChange="pageSizeChange"
          />
        </div>

        <div style="margin-left: 20px; width: 50%" class="f16">
          <span style="font-weight: 600">指令冲裁单价明细建档</span>
          <Table
            :summaryData="summaryData1"
            summary
            :isLoading="isLoading3"
            :tableHeader="tableHeader3"
            :tableData="PunchingUnitPriceFileTableDate"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { ElMessage } from 'element-plus'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'BlankingInstructionUnitPrice'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref, onMounted } from 'vue'
import {
  fecthLotNo,
  lotNoQuery,
  PunchingUnitPriceFile,
  RegeneratesTheInstructionUnitPrice,
  TheInstructionUnitPrice,
  ExportPrice
} from '@/api/ControlDepartment/MaterialManagement/BlankingInstructionUnitPrice'
const isLoading1 = ref(true)
const isLoading2 = ref(true)
const isLoading3 = ref(true)

const summaryData1 = ref<any>([
  {
    index: 5,
    num: 0
  }
])

// 导出
const ExportPriceParms = ref('')

const ExportPriceMethods = async () => {
  const result = await ExportPrice(ExportPriceParms.value)
  if (result.code === 200) {
    ExportPriceParms.value = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 生成指令单价
const TheInstructionUnitPriceParms = ref({
  cfmFlag: '',
  color: '',
  lotNo: '',
  style: ''
})

const TheInstructionUnitPriceMethods = async () => {
  const result = await TheInstructionUnitPrice(TheInstructionUnitPriceParms.value)
  if (result.code === 200) {
    TheInstructionUnitPriceParms.value.lotNo = ''
    lotNoQueryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const clicktogetdata1 = (row: any) => {
  TheInstructionUnitPriceParms.value.cfmFlag = row.cfmFlag
  TheInstructionUnitPriceParms.value.color = row.color
  TheInstructionUnitPriceParms.value.lotNo = row.lotNo
  TheInstructionUnitPriceParms.value.style = row.style
}

// 重新生成指令单价
const RegeneratesTheInstructionUnitPriceParms = ref({
  cfmFlag: '',
  color: '',
  lotNo: '',
  style: ''
})

const RegeneratesTheInstructionUnitPriceMethods = async () => {
  const result = await RegeneratesTheInstructionUnitPrice(RegeneratesTheInstructionUnitPriceParms.value)
  if (result.code === 200) {
    RegeneratesTheInstructionUnitPriceParms.value.lotNo = ''
    PunchingUnitPriceFileMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 置空
const findData = (row: any) => {
  if (row === '生成指令单价') {
    if (TheInstructionUnitPriceParms.value.lotNo === '') {
      JMessage.warning('请选择需要生成指令单价的数据')
    } else {
      TheInstructionUnitPriceMethods()
    }
  }
  if (row === '置空') {
    formList.value.forEach((item) => {
      if (item.type === 'input') {
        item.inputValue = ''
      }
      if (item.type === 'date') {
        item.dateValue = ''
      }
    })
  }
}

const findData1 = async (row: any) => {
  if (row === '置空') {
    formList.value.forEach((item) => {
      if (item.type === 'input') {
        item.inputValue = ''
      }
      if (item.type === 'date') {
        item.dateValue = ''
      }
    })
  }
  if (row === '重新生成指令单价') {
    if (RegeneratesTheInstructionUnitPriceParms.value.lotNo === '') {
      JMessage.warning('请选择需要生成指令单价的数据')
    } else {
      RegeneratesTheInstructionUnitPriceMethods()
    }
  }

  if (row === '导出') {
    if (ExportPriceParms.value === '') {
      JMessage.warning('请选择需要导出的数据')
    } else {
      const res = await fileRequest.get(`/cost/ExportOfPunchingInstructions?lotNo=${ExportPriceParms.value}`)

      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      const contentDisposition = res.headers['Content-Disposition']
      let fileName = `${ExportPriceParms.value}__${Date.now()}.xlsx` // 默认文件名

      if (contentDisposition) {
        const matches = /filename[^*=]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition)
        if (matches != null && matches[1]) {
          fileName = decodeURIComponent(matches[1].replace(/['"]/g, '')) // 去掉引号并解码
        }
      }

      link.download = fileName // 设置下载文件名
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    }
  }
}

// 冲裁单价明细建档
const PunchingUnitPriceFileParms = ref({
  lotNo: ''
})

const clicktogetdata = (row: any) => {
  PunchingUnitPriceFileParms.value.lotNo = row.lotNo
  PunchingUnitPriceFileMethods()

  RegeneratesTheInstructionUnitPriceParms.value.cfmFlag = row.cfmFlag
  RegeneratesTheInstructionUnitPriceParms.value.color = row.color
  RegeneratesTheInstructionUnitPriceParms.value.lotNo = row.lotNo
  RegeneratesTheInstructionUnitPriceParms.value.style = row.style

  ExportPriceParms.value = row.lotNo
}

const PunchingUnitPriceFileTableDate = ref([])

const PunchingUnitPriceFileMethods = async () => {
  isLoading3.value = true
  const result = await PunchingUnitPriceFile(PunchingUnitPriceFileParms.value)
  PunchingUnitPriceFileTableDate.value = result.list
  summaryData1.value[0].num = result.totalCutPrice
  isLoading3.value = false
}

// 指令查询
const lotNoQueryParms = ref({
  inTime: '',
  lotNo: '',
  outTime: '',
  pageNum: 1,
  pageSize: 40,
  sortNo: '',
  styleNo: ''
})

const sortChange1 = (order: any, prop: any, column: any) => {
  if (prop === 'lotNo') {
    if (order === 'ascending') {
      fecthLotNoParms.value.sortNo = 1
    } else if (order === 'descending') {
      fecthLotNoParms.value.sortNo = 2
    } else {
    }
  }
  lotNoQueryMethods()
}

const Launchform = (row: any) => {
  lotNoQueryParms.value.lotNo = row[0].inputValue
  lotNoQueryParms.value.styleNo = row[1].inputValue
  lotNoQueryParms.value.inTime = row[2].dateValue[0]
  lotNoQueryParms.value.outTime = row[2].dateValue[1]
  lotNoQueryMethods()
}

const lotNoQueryParmsTableDate = ref([])

const lotNoQueryMethods = async () => {
  isLoading2.value = true
  const result = await lotNoQuery(lotNoQueryParms.value)
  lotNoQueryParmsTableDate.value = result.list.map((item: any) => {
    if (item.cfmFlag === 'Y') {
      item.backgroundColor = '#00FF00'
    }
    return item
  })
  if (result.list.length !== 0) {
    PunchingUnitPriceFileParms.value.lotNo = result.list[0].lotNo
    PunchingUnitPriceFileMethods()
  }

  total.value = result.total
  isLoading2.value = false
}

const total = ref(0)

const pageChange = (val: any) => {
  lotNoQueryParms.value.pageNum = val
  lotNoQueryMethods()
}

const pageSizeChange = (val: any) => {
  lotNoQueryParms.value.pageSize = val
  lotNoQueryMethods()
}

// 指令查询
const fecthLotNoParms = ref({
  inTime: '',
  lotNo: '',
  outTime: '',
  pageNum: 1,
  pageSize: 40,
  sortNo: -2,
  styleNo: ''
})

const sortChange = (order: any, prop: any, column: any) => {
  if (prop === 'lotNo') {
    if (order === 'ascending') {
      fecthLotNoParms.value.sortNo = 1
    } else if (order === 'descending') {
      fecthLotNoParms.value.sortNo = 2
    } else {
    }
  }

  if (prop === 'style') {
    if (order === 'ascending') {
      fecthLotNoParms.value.sortNo = 3
    } else if (order === 'descending') {
      fecthLotNoParms.value.sortNo = 4
    } else {
    }
  }
  fecthLotNoMethods()
}

const Launchform5 = (row: any) => {
  fecthLotNoParms.value.lotNo = row[0].inputValue
  fecthLotNoParms.value.styleNo = row[1].inputValue
  fecthLotNoParms.value.inTime = row[2].dateValue[0]
  fecthLotNoParms.value.outTime = row[2].dateValue[1]
  fecthLotNoMethods()
}

const fecthLotNoTableDate = ref([])

const fecthLotNoMethods = async () => {
  isLoading1.value = true
  const result = await fecthLotNo(fecthLotNoParms.value)
  fecthLotNoTableDate.value = result.list.map((item: any) => {
    if (item.cfmFlag === null) {
      item.backgroundColor = '#fb83fa'
    }
    return item
  })
  totalL.value = result.total
  isLoading1.value = false
}

const totalL = ref(0)

const pageChangeL = (val: any) => {
  fecthLotNoParms.value.pageNum = val
  fecthLotNoMethods()
}

const pageSizeChangeL = (val: any) => {
  fecthLotNoParms.value.pageSize = val
  fecthLotNoMethods()
}

onMounted(() => {
  fecthLotNoMethods()

  lotNoQueryMethods()
})

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '选择指令 请输入指令',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '选择指令 请输入型体',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'date',
    dateLabel: '派工日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '生成指令单价'
  },
  {
    type: 'button',
    checkboxLabel: '置空'
  }
])

const formList1 = ref([
  {
    type: 'input',
    inputPlaceholder: '指令查询 请输入指令',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '指令查询 请输入型体',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'date',
    dateLabel: '派工日期',
    dateValue: ''
  },

  {
    type: 'button',
    checkboxLabel: '导出'
  },
  {
    type: 'button',
    checkboxLabel: '重新生成指令单价'
  },

  {
    type: 'button',
    checkboxLabel: '置空'
  }
])

const tableHeader = ref([
  {
    label: '指令',
    sortable: true,
    prop: 'lotNo'
  },
  {
    label: '型体',
    sortable: true,
    prop: 'style',
    backgroundColor: true
  },
  {
    label: '颜色代码',
    backgroundColor: true,
    prop: 'colorCode'
  },
  {
    label: '颜色名称',
    prop: 'color'
  }
])

const tableHeader2 = ref([
  {
    label: '指令',
    sortable: true,
    prop: 'lotNo'
  },
  {
    label: '检核状态',
    prop: 'cfmFlag',
    backgroundColor: true,
    width: '50'
  }
])

const tableHeader3 = ref([
  {
    label: '序号',
    prop: 'seqNo'
  },
  {
    label: '部位',
    prop: 'partName'
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    width: '270'
  },
  {
    label: '层数',
    prop: 'cutLayer'
  },
  {
    label: '片数',
    prop: 'pieceNum'
  },
  {
    label: '冲裁单价',
    prop: 'cutPrice'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
</script>

<style lang="less" scoped>
:deep(.el-table .el-table__footer-wrapper) {
  --el-table-row-hover-bg-color: rgb(128, 255, 0);
}
</style>
